<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\Api\UserRequest;
use Carbon\Carbon;

class PassportController extends Controller
{
    public function login(UserRequest $request)
    {
        # 如果登陆成功
        if(auth()->attempt(
            $request->only([
                'phone','password'
            ])
        ))
        {
            $user = auth()->user();
            #记录登陆时间和ip
            $user->old_ip = $user->ip;
            $user->ip = request()->ip();
            $user->old_login_at = $user->login_at;
            $user->login_at = Carbon::now();
            $user->token = $user->createToken('app')->accessToken;
            $user->save();

            $success['token'] = $user->token;
            return $this->succ($success);
        }else{
            return $this->error("账号或者密码错误");  
        }

    }

    public function user()
    {
        return $this->succ(auth()->user());
    }
}
